package com.sghd.candy.module.player.service;

import com.sghd.candy.module.player.manager.Player;
import com.sghd.candy.module.player.manager.PlayerManager;
import com.sghd.pojo.StaticConfig;
import com.sghd.pojo.fight.game.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Allen
 * @date 2018/12/07
 */

@Service
public class PlayerService {
    @Autowired
    private PlayerManager playerManager;

    public Player load(long playerId) {
        return playerManager.load(playerId);
    }

    public Player createPlayer(long playerId) {
        return playerManager.createPlayer(playerId);
    }

    public Result<String> modifyHeadIcon(Player player, int headIcon) {
        player.modifyHeadIcon(headIcon);
        return Result.SUCCESS();
    }

    public Result<String> rename(Player player, String nickName) {
        if (player.rename(nickName)) {
            return Result.SUCCESS();
        } else {
            return Result.valueOf(-1, "已重命名过！");
        }
    }

    public Result<String> guide(Player player, Integer guide) {
        Integer pre = playerManager.loadOther(player.getPlayerId(), StaticConfig.PLAYER_GUIDE, Integer.class);
        if (!pre.equals(guide)) {
            playerManager.updateInfo(player.getPlayerId(), StaticConfig.PLAYER_GUIDE, guide);
        }
        return Result.SUCCESS();
    }


    public <T> T loadOther(Player player, String key, T defaultValue, Class<T> clz) {
        return playerManager.loadOther(player.getPlayerId(), key, defaultValue, clz);
    }
}
